import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-sensor-log',
	templateUrl: './sensor-log.component.html',
})
export class SensorLogComponent implements OnInit {
	tableData: any[] = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	selectedCompanyName: string = ''; //公司树选中公司名称
	selectedCompanyId: string = ''; //公司树选中公司ID
	sortType: string = ''; //传感器重置日志原因
	startTime: string = '';
    endTime: string = '';
	constructor(private jsonApiService: JsonApiService, private router: Router) {}

	ngOnInit() {
		// this.load();
	}
	ngOnDestroy() {
		document.onclick = null;
	}
	//公司选中回调
	companySelected(data) {
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.load();
	}
	//刷新
	refresh() {
		this.load();
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//删除
	del_log(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'logId=' + row.id;
			this.jsonApiService.http("sensorResetLog/deleteForSensorResetLog.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//导出
	export_data() {
		var parameters = {
			searchKey: this.searchKey,
			logType: this.sortType,
			companyId: this.selectedCompanyId,
			startTime: this.startTime,
			endTime: this.endTime
		};
		let timeCheck = common.correctTime(parameters)
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'sensorResetLog/exportSensorResetLogFile.html')
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				searchKey: this.searchKey,
				logType: this.sortType,
				companyId: this.selectedCompanyId,
				startTime: this.startTime,
				endTime: this.endTime
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("sensorResetLog/sensorResetLogForPage.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 接收子组件传递过来的数据
    getSelectTableTime(time) {
        this.startTime = time.startTime;
        this.endTime = time.endTime;
    }
}